﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CSM_SQL;
using CSM_SQL.BLL;

namespace CommunitySuperMarket.Controllers
{
    public class HomeController : Controller
    {
        private const int pageSize = 12;
        private static string path = ConfigurationManager.AppSettings["path"].ToString();
        private static GoodsBLL _gb;
        private GoodsBLL gb
        {
            get
            {
                if (_gb == null)
                    _gb = new GoodsBLL();
                return _gb;
            }
        }

        private CustomerBLL cb = new CustomerBLL();

        public ActionResult Home()
        {
            
                var list = gb.ShowGoods().Where(g => g.grade >= 5).ToList();
                return View(list);
     
            
        }

        public ActionResult Grid(int ? id,int ? page)
        {
 

            if (id == null)
                id = 0;
            if (page == null)
                page = 1;
            int p = Convert.ToInt16(page);
            var list = gb.ShowGoods().Where(g => g.type == id);
            ViewBag.page = p;
            var total = list.Count();
            ViewBag.pages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
            return View(list.Take(p * pageSize).Skip((p - 1) * pageSize).ToList());
        }

        public ActionResult List(int? id,int? page)
        {


            if (id == null)
                id = 0;
            if (page == null)
                page = 1;
            int p = Convert.ToInt16(page);
            var list = gb.ShowGoods().Where(g => g.type == id);
            ViewBag.page = p;
            var total = list.Count();
            ViewBag.pages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
            return View(list.Take(p * pageSize).Skip((p - 1) * pageSize).ToList());
        }

        public ActionResult Cart()
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e == null)
            {
                return RedirectToAction("Login", "Login");

            }
            return View(cb.GetCart(e.C_ID));
        }

        public ActionResult Tasks()
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e == null)
            {
                return RedirectToAction("Login", "Login");

            }
            return View(cb.GetTasks(e.C_ID));
        }

        public ActionResult History()
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e == null)
            {
                return RedirectToAction("Login", "Login");

            }
            return View(cb.GetHistory(e.C_ID).OrderBy(a=>a.order.State).OrderByDescending(a => a.order.StartTime).ToList());
        }

        public ActionResult Evaluate()
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e == null)
            {
                return RedirectToAction("Login", "Login");

            }
            return View(cb.GetHistory(e.C_ID).Where(a=>a.order.State>=3).OrderBy(a => a.order.State).OrderByDescending(a => a.order.EndTime).ToList());
        }

        public ActionResult DelCart(int id)
        {
            cb.DelCart(id);
            return RedirectToAction("Cart");
        }

        public ActionResult ClearCart()
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e == null)
            {
                return RedirectToAction("Login", "Login");

            }
            cb.ClearCart(e.C_ID);
            return RedirectToAction("Cart");
        }

        public void ChangeCart()
        {
            var id = Request.Params["id"];
            var amount = Request.Params["amount"];
            cb.ChangeCart(Convert.ToInt32(id), Convert.ToInt32(amount));
            HttpContext.Response.Write(0);
            HttpContext.Response.End();
        }

        public void Pay()
        {
            var ids = Request.Params["id[]"];
            if(ids == null) {
                HttpContext.Response.Write(0);
                HttpContext.Response.End();
            }
            else
            {
                var Ids = ids.Split(',');
                int[] id = new int[Ids.Length];
                int i = 0;
                foreach(var d in Ids)
                {
                    id[i++] = Convert.ToInt32(d);
                }
                if (cb.Pay(id))
                {
                    HttpContext.Response.Write(0);
                    HttpContext.Response.End();
                }
                else
                {
                    HttpContext.Response.Write(1);
                    HttpContext.Response.End();
                }
            }
            
        }

        public ActionResult Apply(int id)
        {
            var UserID = 0;
            cb.Apply(id, UserID);
            return RedirectToAction("Tasks");
        }

        public ActionResult Send(int id)
        {
            cb.Send(id);
            return RedirectToAction("Tasks");
        }

        public void Pick(int id)
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e == null)
            {
                HttpContext.Response.Write(0);
                HttpContext.Response.End();
            }
            else
            {

                HttpContext.Response.Write(cb.Pick(e.C_ID, id));
                HttpContext.Response.End();
            }
            
        }

        public void CartNum()
        {
            var e = (Customer)HttpContext.Session["CurrUser"];
            if (e == null)
            {
                HttpContext.Response.Write(0);
                HttpContext.Response.End();
            }
            else
            {

                HttpContext.Response.Write(cb.GetCart(e.C_ID).Count());
                HttpContext.Response.End();
            }
        }

        public ActionResult Sign(int id)
        {
            cb.Sign(id);
            return RedirectToAction("History");
        }

        public void PostAssess()
        {
            var a = new Assess
            {
                Assess1 = Request.Params["txt"],
                Grade = Convert.ToInt32(Request.Params["grade"]),
                ID = Convert.ToInt32(Request.Params["id"])
            };
            if (cb.PostAssess(a))
            {
                HttpContext.Response.Write(0);
            }
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();
        }
    }
}